package assist.client.gwt.view.ui;


import assist.client.gwt.service.GamesProvider;
import assist.domain.model.Game;
import assist.domain.model.Games;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusWidget;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.Range;

public class ShowGamesViewImpl extends Composite implements ShowGamesView, ValueChangeHandler<Games>
{
    private Presenter listener;
    @UiField Label title;
    @UiField (provided=true)
    CellList<Game> gamesListUI;
    @UiField FocusWidget selectGamesLink;   

    public ShowGamesViewImpl(GamesProvider gamesProvider, UiBinder<Widget, ShowGamesViewImpl> uiBinder)
    {
        this.gamesListUI = new CellList<Game>(new GameCell());        
        this.gamesListUI.setLoadingIndicator(new Label("Loading"));
        gamesProvider.addDataDisplay(this.gamesListUI);
        this.gamesListUI.setVisibleRange(new Range(0, 100));
        gamesProvider.addGamesChangeHandler(this);
        
        initWidget(uiBinder.createAndBindUi(this));        
    }   
    
    private static class GameCell extends AbstractCell<Game> {

        @Override
        public void render(Context context, Game game, SafeHtmlBuilder sb) {
          /*
           * Always do a null check on the value. Cell widgets can pass null to
           * cells if the underlying data contains a null, or if the data arrives
           * out of order.
           */
          if (game == null) {
            return;
          }

          // If the value comes from the user, we escape it to avoid XSS attacks.
          SafeHtml safeValue = SafeHtmlUtils.fromString(game.getHomeTeamId() +" at "+game.getVisitorTeamId());
       
          sb.appendHtmlConstant("<div>");
          sb.append(safeValue);
          sb.appendHtmlConstant("</div>");
        }
    }    
    
    @UiHandler("selectGamesLink")
    void onClickSelectGames(ClickEvent e)
    {
        listener.goToSelectGames();
    }

    @Override
    public void setPresenter(Presenter listener)
    {
        this.listener = listener;
    }

    @Override
    public void onValueChange(ValueChangeEvent<Games> event)
    {
             if (event.getValue().getGamesList() == null)
        {
            this.title.setText("Loading");
            this.gamesListUI.setVisible(false);
        }
        else
        {
            this.title.setText("Showing:" +event.getValue().getGamesList().size() + " Games");
            this.gamesListUI.setVisible(true);
        }
                 
    }
    

    @Override
    public void setEditMode(boolean mode)
    {
        selectGamesLink.setVisible(!mode);      
    }     
}
